import axios from "axios";
import {ElMessage} from "element-plus";

export class BaseService {
    constructor(root) {
        // service root为后端服务接口的同一根目录（一个Controller对应一个Service文件）
        this.root = root;
    }

    async _sendRequest(path, payload, method = 'post') {
        try {
            const url = `${this.root}${path}`;
            let response = await axios.post(url, payload);
            if (response.data.content == null) {
                throw new Error(`请求失败: ${response.data.code}|${response.data.message}`);
            }
            return response.data.content;
        } catch (e) {
            // 异常转发给调用方
            ElMessage.error(e.message);
            throw e;
        }
    }
}